#include <stdio.h>
#include <math.h>
#include "user.h"
#include "user_doe.h"

float funct(float t, float sigma){
				  float res;
				  res=sqrt(1-exp(-t*t/(sigma*sigma)));
				  return res;
				 }


float integral(float r, float sigma){
float s,t, delta=r/200;
int i;
s=0;
for(i=0;i<200;i++){
		   t=delta*i;
		   s=s+funct(t,sigma)*delta;
		  }
return s;
}

static double R,R0,f,max,min,wl,step,sigma;

//R-aperture radius
//R0-focal ring radius
//f-focal distance
//sigma-radius osveschayushego puchka
//wl - wavelength



double UserAnn1Fun(double r){
			     float ph,k;
			     k=2*M_PI/wl;
			     ph=0;
			     //ph=-k*r*r/(2*f);
			    ph=ph+k*R0*integral(r,sigma)/f;
			    return ph+k*R*R/(2*f);
			    return ph;
			    }


void UserAnnular1()   {
char *InputValues[6];
   char *Title={" DOE : Focusator Into Around Spot"};
   char *ParNames[]={
		     "Aperture Radius    :",
		     "Wavelength         :",
		     "Focal Distance	 :",
		     "Focal Spot Radius  :",
		     "Gaussian   Radius  :",
		     "Discretization Step:"
		    };
   char *Default[]={"12.8       ",
		    "1.06       ",
		    "200        ",
		    "0.4        ",
		    "6          ",
		    "0.1        "
		   };
   if(!sedit(25,5,6,ParNames,InputValues,Default,Title))  return ;

   // read input data

   sscanf(InputValues[0],"%lf \0",&R);
   sscanf(InputValues[1],"%lf \0",&wl);
   wl=wl/1000;
   sscanf(InputValues[2],"%lf \0",&f);
   sscanf(InputValues[3],"%lf \0",&R0);
   sscanf(InputValues[4],"%lf \0",&sigma);
   sscanf(InputValues[5],"%lf \0",&step);

      max=UserAnn1Fun(0);
      min=UserAnn1Fun(R);

   AnnularDOE(Phase, max, min, UserAnn1Fun);

   if(MessageSize(2*R, 2*R, step)) return;

   // Output information to InfoWindow
   MessageName("Gaussian Wavefront");
   MessageParam(3,"Aperture Radius=%f",R);
   MessageParam(4,"Step =%f",step);
   MessageParam(5,"Wavelength=%f",wl);

   MessageOut();               // output all string to InfoWindow

   UndoMenu(3) ;   //  delete 3 last level submenu on the screen
   return;
}
